package com.example.pattern.draw

import android.content.Context
import android.util.AttributeSet
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.children

class ToolConstainerView(context: Context,attrs:AttributeSet?):ConstraintLayout(context, attrs) {
    private var mLastSelectView:ToolView? = null
    var viewModel:DrawViewModel? = null
    override fun onFinishInflate() {
        super.onFinishInflate()
        //给所有的子控件添加回调事件
        children.forEach {
                if (it !is ImageView ){
                    val child = it as ToolView
                    child.addToolClickListener = {view,tooltype->
                        if(tooltype == ToolType.None){
                            mLastSelectView = null
                        }else{
                            mLastSelectView?.changeSelectedState(false)
                            mLastSelectView = view
                        }
                        viewModel?.let {
                           it.setType(tooltype)
                        }
                }
            }
        }
    }
}